package com.l2device.gameserver.communitybbs.custom;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javolution.text.TextBuilder;

import com.l2device.gameserver.L2DatabaseFactory;

public class TopClans
{
	private final TextBuilder list = new TextBuilder();
	
	public TopClans()
	{
		load();
	}
	
	private void load()
	{	
		try (Connection con = L2DatabaseFactory.getInstance().getConnection())
		{
				PreparedStatement statement = con.prepareStatement("SELECT clan_name, clan_level, reputation_score, ally_name FROM clan_data WHERE clan_level>" + 5 + "");
				ResultSet result = statement.executeQuery();

				int pos = 0;
				
				while (result.next())
				{
					pos++;
					String clan_name = result.getString("clan_name");
					int level = result.getInt("clan_level");
					int reputation = result.getInt("reputation_score");
					String ally = result.getString("ally_name");

					addClanToList(pos, clan_name, level, reputation, ally);
				}
				result.close();
				statement.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void addClanToList(int pos, String clan_name, int level, int reputation, String ally)
	{
		list.append("<table border=0 cellspacing=0 cellpadding=2 width=750>");
		list.append("<tr>");
		list.append("<td FIXWIDTH=2></td>");
		list.append("<td FIXWIDTH=15>" + pos + "</td>");
		list.append("<td FIXWIDTH=30>" + clan_name + "</td>");
		list.append("<td FIXWIDTH=30>" + level + "</td>");
		list.append("<td FIXWIDTH=30>" + reputation + "</td>");
		list.append("<td FIXWIDTH=30>" + ally + "</td>");
		list.append("<td FIXWIDTH=2></td>");
		list.append("</tr>");
		list.append("</table>");
		list.append("<img src=\"L2UI.Squaregray\" width=\"740\" height=\"1\">");
	}
	
	public String loadList()
	{
		return list.toString();
	}
}